import { Meteor } from 'meteor/meteor';
import '../imports/api/users/users.js';
import  {fake} from './fake.js';
import '../imports/api/methods.js';
import {Users} from '../imports/api/users/users.js';
//import {HfcOperate} from './hfc/hfcHelper.js';
import {wsRest} from './hfc/ws_hfc.js';
import {HfcRest} from './hfc/hfcHelper4Rest.js';
import {mycbops} from '../imports/api/blocksmgr/chainblocks4mongo.js';


/*function initblock(){
              console.log("get blocks start .......");
                var mybs = wsRest.getBlocks();
                //console.log(bs);
                let i = 0;
                for(i = 0; i < mybs.length-1; i++){
                    let tmpblock = mybs[i];
                    console.log(tmpblock);
                    let h = tmpblock.blockheight;
                    let qstr = {"blockheight":h.toString()};
                    let tmprs = null;
                    try{
                        //let all = mycbops.find().fetch();
                        //if(all.length >  0){
                            tmprs = mycbops.find(qstr).fetch();
                       // }
                    }catch(e){
                        tmprs = null;
                    }
                    //console.log("rs cccccccccccccc="+JSON.stringify(tmprs));
                    if(tmprs == null || tmprs.length == 0){
                        mycbops.insert(tmpblock);
                        console.log("insert blocks "+tmpblock);
                    }
                    };
                console.log("get blocks end .......");
}

function monitorblock(){
    setInterval(function(){
                initblock();
                }, 10000);
}*/



Meteor.startup(() => {
  // code to run on server at startup
  GV_defaultUser = fake();
 
 
  HfcRest.startup_fabric();
  console.log('main:'+GV_defaultUser);
  //initblock();
  /*var tmp = cbops.find({"blockheight":2}).fetch();
  if(tmp != null){
    console.log("//////////////="+tmp.length);
  }
  console.log('0000000000000000000000='+JSON.stringify(tmp));*/
  //setInterval(function(){getAllBlocks();}, 10000);

});

Meteor.methods({
  'account.currentUser'() {
    return GV_defaultUser;
  },
  
  'account.setCurrentUser'(cusr) {
    GV_defaultUser=cusr;
    return cusr;
  },

  'account.login1'(usrname,pwd) {
    query= {"userId":usrname};
    console.log("entry");
    fus=[];
    //console.debug(usrname);
    //debugger;
    fus = Users.find(query).fetch();
    console.log("entry1");
    if(fus.length == 1){
       var muser = fus[0];
       var loguser = wsRest.registerUser(usrname,pwd);
      
      console.log("entry login 3");
      if(loguser != null){
        console.log("entry login 4");
        muser.security = pwd;
        console.log("entry login 5");
        console.log("login success");
        console.log(muser);
        console.log("entry login 7777");
        return muser;
      }else{
          console.log("login failed");
      }  
  }

},

  /*'account.login1'(usrname,pwd) {
    //return sysLogin (usrname,pwd);
    query=//{
            //$and: 
           // [
              {"userId":usrname}//,  
           //   {"password":pwd} 
           // ]
          //}
  console.log("entry");
   fus=[];
   //console.debug(usrname);
   //debugger;
   fus = Users.find(query).fetch();
   console.log("entry1");
   if(fus.length == 1){
       var muser = fus[0];
       HfcOperate.InitChain();
      var loguser = HfcOperate.userLogin4Synch(usrname,pwd);
      console.log("entry login 3");
      if(loguser != null){
        console.log("entry login 4");
        muser.security = pwd;
        console.log("entry login 5");
        console.log("login success");
        console.log(muser);
        console.log("entry login 7777");
        return muser;
      }else{
          console.log("login failed");
      }  
  }
  },*/

/*
'account.join1'(usrname,pwd) {
    //return sysLogin (usrname,pwd);
    var tuser = {
    "title" : usrname,
    "createdAt" : new Date(),
    "userId" : usrname,
    "avatar" : "https://s3.amazonaws.com/uifaces/faces/twitter/balintorosz/128.jpg",
    "status" : "已认证",
    "utype"  : "用户",
    "desc"   : "Global",
    "createdBy" : "",
    "balance" : 0
};
    query=//{
           // $and: 
          //  [
              {"userId":usrname};//,  
          //    {"password":pwd} 
          //  ]
          //}
   fus=[];
   //console.debug(usrname);
   //debugger;
   console.log("entry");
   fus = Users.find(query).fetch();
   console.log("entry1");
   if(fus.length <= 0){
     console.log("entry2");
       HfcOperate.InitChain();
      var reguser = HfcOperate.userRegiste4Synch(usrname,pwd,"bank_a");
      if(reguser != null){
        return Users.insert(tuser);
        //return muser;
      }else{
          console.log("reg failed");
      }  
  }
}*/


});